package xyz.wanwanwan.www.hrm.service.impl;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import xyz.wanwanwan.www.hrm.dto.TenantDTO;
import xyz.wanwanwan.www.hrm.mapper.EmployeeMapper;
import xyz.wanwanwan.www.hrm.mapper.TenantMapper;
import xyz.wanwanwan.www.hrm.service.ITenantService;
import xyz.wanwanwan.www.hrm.util.StrUtils;
import xyz.wanwanwan.www.hrm.util.encrypt.MD5;
import xyz.wanwanwan.www.hrm.domain.Employee;
import xyz.wanwanwan.www.hrm.domain.Meal;
import xyz.wanwanwan.www.hrm.domain.Tenant;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Fu
 * @since 2020-09-10
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Value("${hrm.tenant.testdays}")
    private int testdays;
    @Override
    public void entering(TenantDTO dto) {
        //获取当前时间
        Date now = new Date();
        Long mealId = dto.getMealId();
        //获取员工
        Employee employee = dto.getEmployee();
        //获取租户
        Tenant tenant = dto.getTenant();
        //为租户设置注册时间
        tenant.setRegisterTime(now);
        //为租户设置状态
        tenant.setState(false);
        baseMapper.insert(tenant);
        //关联机构id
        employee.setTenantId(tenant.getId());
        //盐值
        String salt = StrUtils.getComplexRandomString(10);
        employee.setSalt(salt);
        //加盐密码
        String pw = MD5.getMD5(employee.getPassword() + salt);
        employee.setPassword(pw);
        employee.setRealName(null);
        employee.setInputTime(now);
        //员工状态0正常，1锁定，2注销',
        employee.setState(Employee.STATE_NORMAL);
        //员工类型 1普通员工 ，2客服人员，3管理员
        employee.setType(Employee.ADMAIN);
        employeeMapper.insert(employee);
        //套餐关系
        //设置过期时间
        Date expireDate = DateUtils.addDays(now, testdays);
        baseMapper.insertRelationMeal(mealId,tenant.getId(),expireDate, Meal.NO_PAY);



    }
}
